﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using log4net;
using RealtyManagementSystem.BLL.Fee;

namespace RealtyManagementSystem.Web
{
    public class Global : System.Web.HttpApplication
    {
        private void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            ILog gloLogger = log4net.LogManager.GetLogger("DtWebLogger");
            // 在应用程序启动时运行的代码

            if (!Roles.RoleExists("Administrators"))
            {
                Roles.CreateRole("Administrators");
            }

            if (!Roles.RoleExists("Householders"))
            {
                Roles.CreateRole("Householders");
            }

            if (!Roles.RoleExists("RealtyManagers"))
            {
                Roles.CreateRole("RealtyManagers");
            }

            using (var context = new FeeEntities())
            {
                var caCount = (from fc in context.FeeCategories
                               select fc).Count();
                if (caCount == 0)
                {
                    var feeCategory1 = new FeeCategory()
                    {
                        CategoryName = "PropertiesFee"
                    };
                    var feeCategory2 = new FeeCategory()
                    {
                        CategoryName = "ParkingsFee"
                    };
                    var feeCategory3 = new FeeCategory()
                    {
                        CategoryName = "ShoplotsFee"
                    };

                    context.FeeCategories.Add(feeCategory1);
                    context.SaveChanges();
                    gloLogger.Info("sava!");
                }
            }
        }

        private void Application_End(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码
        }

        private void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

            // Get the exception object.
            Exception exc = Server.GetLastError();
            // Log the exception and notify system operators
            log4net.ILog logger = log4net.LogManager.GetLogger("DtWebLogger");

            logger.Error("Global Error", exc);

            // Handle HTTP errors
            if (exc.GetType() == typeof(HttpException))
            {
                // The Complete Error Handling Example generates
                // some errors using URLs with "NoCatch" in them;
                // ignore these here to simulate what would happen
                // if a global.asax handler were not implemented.
                if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
                    return;

                //Redirect HTTP errors to HttpError page
                Server.Transfer("HttpErrorPage.aspx");
            }

            Server.Transfer("GlobalErrorPage.aspx");

            // For other kinds of errors give the user some information
            // but stay on the default page
            Response.Write("<h2>Global Page Error</h2>\n");
            Response.Write(
                "<p>" + exc.Message + "</p>\n");
            Response.Write("Return to the <a href='Default.aspx'>" +
                "Default Page</a>\n");

            // Clear the error from the server
            Server.ClearError();
        }

        private void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码
        }

        private void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时，才会引发 Session_End 事件。如果会话模式设置为 StateServer
            // 或 SQLServer，则不会引发该事件。
        }
    }
}